package com.csjbot.weedle_auto.SerialServer.util.bean


import com.csjbot.weedle_auto.util.Csjlogger
import java.util.*

/**
 * Created by Administrator on 2016/6/24 0024.
 * 数据头	数据流向	消息参考	命令值	  数据长度    数据      校验和
 * header      src       ref        cmd       dataLen    data       check
 * 0xAA        ？	      ？	     ？          ？	       ？	      ？
 *
 *
 *
 *
 * 用法 :
 * 获取数据
 * SerialPacket packet = new SerialPacket(data[])
 * 发送数据
 * byte[] bt = {0x21, 0x32}
 * SerialPacket packet = new SerialPacket(dataType, subType, bt)
 * byte[] out = packet.packetToData();
 * send(out);
 */
class SerialPacket {
    var scr: Byte = 0
        private set
    var ref: Byte = 0
        private set
    var cmd: Byte = 0
        private set
    var dataLen: Byte = 0
        private set
    lateinit var data: ByteArray

    //    private byte check = 0x00;


    constructor(comingData: ByteArray) {
        // Logger.d("chenqi SerialPacket" + comingData.length);
        scr = comingData[1]
        ref = comingData[2]
        cmd = comingData[3]
        dataLen = comingData[4]
        if (dataLen < 0) {
            return
        }
        data = ByteArray(dataLen.toInt())
        try {
            System.arraycopy(comingData, 5, data, 0, dataLen.toInt())
        } catch (e: ArrayIndexOutOfBoundsException) {
            Csjlogger.error("rec serial error {}",
                    Arrays.toString(comingData))
        }

    }


    /**
     * @author Administrator
     * @time 2016/6/24 0024 下午 1:01
     */
    constructor(scr: Byte, ref: Byte, cmd: Byte, dataLen: Byte) {
        this.scr = scr
        this.ref = ref
        this.cmd = cmd
        this.dataLen = dataLen
        this.data = ByteArray(this.dataLen.toInt())
        System.arraycopy(data, 0, this.data, 0, dataLen.toInt())
    }

    /**
     * @author Administrator
     * @time 2016/6/24 0024 下午 1:01
     */
    constructor(scr: Byte, ref: Byte, cmd: Byte, data: ByteArray, dataLen: Byte) {
        this.scr = scr
        this.ref = ref
        this.cmd = cmd
        this.dataLen = dataLen
        this.data = data
        System.arraycopy(data, 0, this.data, 0, dataLen.toInt())
    }


    private val check: Byte
        get() {
            val dataSum = (0 until dataLen).sumBy { data[it].toInt() }
            return (SerialPortConstants.header.toInt() + scr.toInt() + ref.toInt() + cmd.toInt() + dataLen.toInt() + dataSum and 0xFF).toByte()
        }

    val bytes: ByteArray
        get() {
            val out = ByteArray(6 + dataLen)
            var offset = 0
            System.arraycopy(byteArrayOf(SerialPortConstants.header), 0, out, offset, 1)
            offset += 1
            System.arraycopy(byteArrayOf(scr), 0, out, offset, 1)
            offset += 1
            System.arraycopy(byteArrayOf(ref), 0, out, offset, 1)
            offset += 1
            System.arraycopy(byteArrayOf(cmd), 0, out, offset, 1)
            offset += 1
            System.arraycopy(byteArrayOf(dataLen), 0, out, offset, 1)
            offset += 1

            if (dataLen > 0) {
                System.arraycopy(this.data, 0, out, offset, dataLen.toInt())
                offset += dataLen.toInt()
            }

            System.arraycopy(byteArrayOf(check), 0, out, offset, 1)
            return out
        }

}
